﻿using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SubstrateHelper.Helpers
{
    public class UserHelper
    {
        //public static async Task<string> GetUserName()
        //{
        //    using (var scope = Global.Host.Services.CreateScope())
        //    {
        //        var services = scope.ServiceProvider;
        //        var authenticationStateProvider = services.GetRequiredService<AuthenticationStateProvider>();
        //        var UserManager = services.GetRequiredService<UserManager<IdentityUser>>();
        //        var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
        //        var User = authState.User;
                
        //        return UserManager.GetUserId(User);
        //    }
        //}

        public static bool HasRole(string user,string role)
        {
            //var user = await GetUserName();
            using (var scope = Global.Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;                
                var Db = services.GetRequiredService<SubstrateHelper.Data.SubstrateHelperDbContext>();

                var a = Db.UserRoles.Where(x => x.UserId == user && x.RoleId == role).FirstOrDefault();
                if (a == null)
                {
                    return false;
                }
                return true;
            }
            
        }
    }
}
